// pages/components/Collect/collect.js
// 调用时传递两个参数 一个是顶部的名字，一个是类型（移动/收藏）
import { request, fomatForCollect } from '../../../../utils/util'

const { $Message } = require('../../../../dist/base/index');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isNeedAction: false, //用来判断是否显示三个点
    fileList: [],
    isLoading: true,
    classifyType: "",
    key: "",
    fileListHeight: app.globalData.fileListHeight + "px",
    fileKey: "",//文档或文件的id或key
    type: "",
    backPageClassify: "",//用来判断退出页面层数
    projectId: "",//查询项目的目录用得到
    firstKey:""  //第一次移动或复制的key
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const classifyType = options.classifyType
    const projectId = options.id
    let   firstKey = "";
    if(options.time === "第一次"){
       firstKey = options.key  //用于根目录移动复制所要用的parent
    }
   
    const a = wx.getStorageSync('userInfo');
    const catalog_user = a.catalog
    let key = "";
    if (classifyType === "org") {
      key = options.key
    
    }
    if (classifyType === "user") {
      if (options.key) {
        key = options.key
      } else {
        key = catalog_user
      }
    }
    if (classifyType === "project") {
      if(options.time === "第一次"){
        key = options.key
      }else{
        key = options.key 
      }
      
    }

    const fileKey = app.globalData.moveAndCopy.fileKey
    const type = app.globalData.moveAndCopy.type
    const optionType = options.type
    const backPageClassify = options.backPageClassify
    

    this.setData({
      ...this.data,
      classifyType: classifyType,
      key: key,
      type: optionType,
      backPageClassify: backPageClassify,
      projectId: projectId,
      firstKey:firstKey
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getFileList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**
 * 获取数据的封装
 */
  getFileList() {
    const that = this

    if (this.data.classifyType === "project") {
      let param = {}
      if (this.data.key === "") {
        param = {
          type: this.data.classifyType,
          project: this.data.projectId
        }
      } else {
        param = {
          type: this.data.classifyType,
          parent: this.data.key,
          project: this.data.projectId
        }
      }
      request("GET", "/api/service/catalog", param).then(res => {
        const data = res.data.data;
        const newData = that.filter(data)
       
        that.setData({
          fileList: newData,
          isLoading: false
        })
      })
    } else {
      //key区分根目录还是不是根目录
      if (this.data.key === undefined) {
        const param = {
          type: this.data.classifyType,
        }
        request("GET", "/api/service/catalog", param).then(res => {
          const data = res.data.data;
          const newData = that.filter(data)
          
          that.setData({
            fileList: newData,
            isLoading: false
          })
          
        })
      } else { //不是根目录
        const param = {
          type: this.data.classifyType,
          parent: this.data.key
        }
        request("GET", "/api/service/catalog", param).then(res => {
          const data = res.data.data;
          const newData = that.filter(data)
         
          that.setData({
            fileList: newData,
            isLoading: false
          })
         
        })
      }
    }

  },
  /**
   * 新建文件夹
   */
  createFolder() {
    this.selectComponent("#inputModal").setShow()
  },
  /**
   * 空方法，为了不修改结构
   */
  handleCancel1() {

  },
  /**
   * 确定
   */
  sure() {
    //key区分根目录还是不是根目录
    const that = this
    const page = getCurrentPages()
    let num = "";
    //从企业里 去到个人的时候  不能正常退回页面 =》已找到原因为 classifyType有问题
    if (this.data.backPageClassify === "org") {
      num = page.length - 1;
    };
    if (this.data.backPageClassify === "project") {
      num = page.length - 2
    }
    if (this.data.backPageClassify === "user") {

      num = page.length - 2
    }

    //以上用来控制完成后回退的页面，因为进的层级不一样，所以调的不一样
    const fileKey = [app.globalData.moveAndCopy.fileKey]
    const type = app.globalData.moveAndCopy.type
    
    if (this.data.key === undefined) {
      $Message({
        content: "不能移动/复制到根目录",
        type: 'warning'
      });
    } else {
      if (type == "source_doc") {
        let param = {}
        if(this.data.firstKey){
          param = {
            source_doc: fileKey,
            type: this.data.type,
            arrive_catalog: this.data.firstKey
          }
        }else{
          param = {
            source_doc: fileKey,
            type: this.data.type,
            arrive_catalog: this.data.key
          }
        }
        request("POST", "/api/service/move", param,"",true,`${that.data.type}中，请稍等`)
          .then(res => {
            if (res.statusCode === 200) {
              wx.navigateBack({
                delta: num,
                success: res => {
                  const showMsg = setTimeout(() => {
                    $Message({
                      content: that.data.type + '成功',
                      type: 'success',

                    });
                    clearTimeout(showMsg)
                  }, 500)
                }
              })
            }
          })
      } else {
        let param = {}
        if(this.data.firstKey){
          
          param = {
            source_catalog: fileKey,
            type: this.data.type,
            arrive_catalog: this.data.firstKey
          }
        }else{
          
          param = {
            source_catalog: fileKey,
            type: this.data.type,
            arrive_catalog: this.data.key
          }
        }
        request("POST", "/api/service/move", param,"",true,`${that.data.type}中，请稍等`)
          .then(res => {
            if (res.statusCode === 200) {
              wx.navigateBack({
                delta: num,
                success: res => {
                  const showMsg = setTimeout(() => {
                    $Message({
                      content: that.data.type + '成功',
                      type: 'success',

                    });
                    clearTimeout(showMsg)
                  }, 500)
                }
              })
            }
          })
      }
    }

  },
  /**
   * 过滤器
   */
  filter(data) {
    const newData = [];
    data.forEach(element => {
      if (element.types === "folder") {
        newData.push(element)
      }
    });
    return newData
  }


})